home *** CD-ROM | disk | FTP | other *** search
- // ******************************************************************
- // program written by
- // Paul Baxter
- // MacHack'98
- //
- //
- // ******************************************************************
-
- #include <A4Stuff.h>
- #include <StdLib.h>
-
- #include "patch.h"
- #include "animate.h"
- // #include "debug.h"
-
- // Semi- Automagic macros for generic Fat patches
- #define PatchTrap _ShowHide
-
- #define kPatchType68K 'PT68'
- #define kPatchTypePPC 'PTPP'
-
- #define PatchUPPProcInfo ThePatchuppProcInfo
- #define PatchUPPProcPtr ThePatchProcPtr
- #define PatchFun ThePatch
-
- #ifdef powerc
- enum {
- PatchUPPProcInfo = kPascalStackBased
- | RESULT_SIZE(SIZE_CODE(kNoByteCode))
- | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(WindowPtr)))
- | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(Boolean)))
- };
- typedef UniversalProcPtr PatchUPP;
- #define NEW_FATPROC(proc68K, procPPC) (PatchUPP) \
- NewFatRoutineDescriptor((ProcPtr)(proc68K), (ProcPtr)(procPPC), PatchUPPProcInfo)
- #define NEW_PROC(proc) (PatchUPP) \
- NewRoutineDescriptor((ProcPtr)(proc), PatchUPPProcInfo, GetCurrentArchitecture())
- #define NEW_68KPROC(proc) (PatchUPP) \
- NewRoutineDescriptor((ProcPtr)(proc), PatchUPPProcInfo, kM68kISA);
- #define CALL_PROC(proc, p1, p2) \
- CallUniversalProc((UniversalProcPtr) (proc) , PatchUPPProcInfo, p1, p2)
- #else
- enum {
- PatchUPPProcInfo = 0
- };
- typedef pascal void (*PatchUPPProcPtr)(WindowPtr, Boolean);
- typedef PatchUPPProcPtr PatchUPP;
- #define NEW_PROC(proc) (PatchUPP)(proc)
- #define CALL_PROC(proc, p1, p2) (* (PatchUPPProcPtr) (proc))(p1, p2)
- #endif
-
- // Function Prototypes
- pascal ProcPtr main(PlaySoundUPP soundPlayer68K, PlaySoundUPP soundPlayerPPC, short rID, short install);
- pascal void PatchFun(WindowPtr theWindow, Boolean show);
- void memclear(void* memptr, long len);
-
- // Globals
- PlaySoundUPP gSoundPlayer = nil;
- PatchUPP gOriginal = nil;
- Boolean gGoAwayCalled = false;
-
- #ifdef powerc
- // for Metrowerks' linker, this defines the interface for main().
- ProcInfoType __procinfo = PatchInstallerUPPProcInfo;
- #endif
-
- // * ******************************************************************************
- // * main
- // * Entry point for the ShowHide Patch Installer
- // * ******************************************************************************
- pascal ProcPtr main(PlaySoundUPP soundPlayer68K, PlaySoundUPP soundPlayerPPC, short rID, short install)
- {
- #ifndef powerc
- #pragma unused(soundPlayerPPC, rID)
- #else
- Handle theHandle;
- ProcPtr the68KProc;
- PatchInstallerUPP patchInstaller68K;
- #endif
-
- PatchUPP Patch;
- THz theZone;
- ProcPtr theProc;
-
- EnterCodeResource();
-
- theZone = GetZone();
- SetZone(SystemZone());
-
- // DEBUG("Enter ShowHide Installer");
-
- Patch = nil;
- theProc = (ProcPtr)PatchFun;
- gSoundPlayer = soundPlayer68K;
- gOriginal = (PatchUPP)
- NGetTrapAddress(PatchTrap, (PatchTrap & 0x0800) ? ToolTrap : OSTrap);
-
- #ifdef powerc
- if (soundPlayerPPC)
- gSoundPlayer = soundPlayerPPC;
- else
- if (gSoundPlayer->goMixedModeTrap != _MixedModeMagic)
- gSoundPlayer = NEW_68KPLAYSOUNDPROC(gSoundPlayer);
-
- if (gOriginal->goMixedModeTrap != _MixedModeMagic)
- gOriginal = NEW_68KPROC(gOriginal);
- #endif
-
- if (!install) {
- // DEBUG("Exit ShowHide Installer (no Install)");
-
- SetZone(theZone);
- ExitCodeResource();
- return theProc;
- }
-
- #ifdef powerc
- theHandle = GetResource(kPatchType68K, rID);
- if (theHandle) {
-
- LoadResource(theHandle);
- HLockHi(theHandle);
- DetachResource(theHandle);
-
- the68KProc = 0;
- patchInstaller68K = NEW_68K_PATCH_INSTALLER_PROC(*theHandle);
- the68KProc = (ProcPtr) CALL_PATCH_INSTALLER_PROC(patchInstaller68K, soundPlayer68K, soundPlayerPPC, rID, false);
- DisposeRoutineDescriptor(patchInstaller68K);
-
- if (the68KProc)
- Patch = NEW_FATPROC(the68KProc, theProc);
- }
- #endif
-
- if (!Patch)
- Patch = NEW_PROC(theProc);
-
- NSetTrapAddress((UniversalProcPtr) Patch, PatchTrap, (PatchTrap & 0x0800) ? ToolTrap : OSTrap);
-
- SetZone(theZone);
-
- // DEBUG("Exit ShowHide Installer");
-
- ExitCodeResource();
- return theProc;
- }
-
- // * ******************************************************************************
- // * ThePatch
- // * ShowHide Patch Entry Point
- // * ******************************************************************************
- pascal void PatchFun(WindowPtr theWindow, Boolean show)
- {
-
- ProcessSerialNumber PSN;
- ProcessInfoRec info;
- Point savePt;
- long saveRefCon;
- OSErr err;
-
- EnterCodeResource();
-
- saveRefCon = ((WindowPeek)theWindow)->refCon;
-
- if (!gGoAwayCalled && !show && saveRefCon == kPatchSigniture)
- gGoAwayCalled = true;
-
- if (!gGoAwayCalled) {
- memclear(&info, sizeof(info));
- info.processInfoLength = sizeof(info);
- err = GetFrontProcess(&PSN);
- if (!err) {
- memclear(&info, sizeof(info));
- info.processInfoLength = sizeof(info);
- err = GetProcessInformation(&PSN,&info);
- if ((!err) && (info.processSignature != 'MACS'))
- gGoAwayCalled = true;
- }
- }
-
- if (((WindowPeek)theWindow)->refCon != kPatchSigniture) {
- if ((show == false) && ((WindowPeek)theWindow)->visible) {
- ((WindowPeek)theWindow)->refCon = kPatchSigniture;
- if (gSoundPlayer) {
- CALL_SOUNDPROC(gSoundPlayer);
- }
- AnimateWindow(theWindow, &savePt);
- }
- }
- CALL_PROC(gOriginal, theWindow, show);
- if (gGoAwayCalled && (((WindowPeek)theWindow)->refCon != kPatchSigniture) && (show) && ((WindowPeek)theWindow)->visible) {
- ((WindowPeek)theWindow)->refCon = kPatchSigniture;
- if (gSoundPlayer) {
- CALL_SOUNDPROC(gSoundPlayer);
- AnimateWindow(theWindow, &savePt);
- CALL_PROC(gOriginal, theWindow, show);
- }
- }
- ((WindowPeek)theWindow)->refCon = saveRefCon;
-
- ExitCodeResource();
- }
-
- // * ******************************************************************************
- // * ThePatch
- // * Clear a block of memory
- // * ******************************************************************************
- void memclear(void* memptr, long len)
- {
- long count, finishcount;
- double* curPtr;
- Ptr finishPtr;
-
- count = len / sizeof(double);
- finishcount = len % count;
-
- curPtr = (double*)memptr;
- while (count--)
- *curPtr++ = 0.0;
-
- finishPtr = (Ptr)curPtr;
- while (finishcount--)
- *finishPtr++ = 0;
- }
-